//
//  ZAlert.m
//
//  Created by MacBook on 2018/1/21.
//  Copyright © 2018年 ZhàoYǒng. All rights reserved.
//

#import "ZAlert.h"
#import <Masonry/Masonry.h>

@interface ZAlert ()

@property (nonatomic, assign) BOOL lightBackground;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

@end

static ZAlert *zAlertInstance = nil;

@implementation ZAlert

+ (instancetype)share {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        zAlertInstance = [[ZAlert alloc] init];
    });
    return zAlertInstance;
}

+ (void)setLightBackground:(BOOL)lightBackground {
    ZAlert.share.lightBackground = lightBackground;
}

+ (void)openAlert:(UIView *)alertContent allowTapToHide:(BOOL)allowTapToHide {
    [ZAlert.share openAlert:alertContent allowTapToHide:allowTapToHide];
}

+ (void)hide {
    [ZAlert.share hide];
}

- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardFrameChanged:) name:UIKeyboardWillChangeFrameNotification object:nil];
        UIView *tapMask = [[UIView alloc] init];
        tapMask.backgroundColor = UIColor.clearColor;
        [self addSubview:tapMask];
        [tapMask mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [tapMask addGestureRecognizer:self.tapGesture];
    }
    return self;
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self hide];
    }
}

- (void)keyboardFrameChanged:(NSNotification *)notification {
    CGFloat y = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
    CGRect rect = [self convertRect:self.contentView.frame toView:UIApplication.sharedApplication.keyWindow];
    CGFloat delta = CGRectGetMaxY(rect) + 20.f - y;
    if (y == UIScreen.mainScreen.bounds.size.height) {
        [UIView animateWithDuration:0.2f animations:^{
            self.contentView.transform = CGAffineTransformIdentity;
        }];
    } else {
        if (CGAffineTransformEqualToTransform(self.contentView.transform, CGAffineTransformIdentity) && delta < 0) return;
        [UIView animateWithDuration:0.2f animations:^{
            self.contentView.transform = CGAffineTransformTranslate(self.contentView.transform, 0, - delta);
        }];
    }
}

- (void)openAlert:(UIView *)alertContent allowTapToHide:(BOOL)allowTapToHide {
    self.contentView = alertContent;
    self.tapGesture.enabled = allowTapToHide;
    
    UIView *aView = UIApplication.sharedApplication.keyWindow;
    self.frame = aView.bounds;
    if (!self.lightBackground) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    }
    [aView addSubview:self];
    
    [self addSubview:self.contentView];
    self.contentView.center = self.center;
    self.contentView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    self.contentView.alpha = 0;
    
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:1.f initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        if (!self.lightBackground) {
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        }
        self.contentView.transform = CGAffineTransformIdentity;
        self.contentView.alpha = 1.f;
    } completion:nil];
    
}

- (void)hide {
    
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:1.f initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        if (!self.lightBackground) {
            self.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
        }
        self.contentView.transform = CGAffineTransformMakeScale(0.3, 0.3);
        self.contentView.alpha = 0.f;
    } completion:^(BOOL finished) {
        [self.contentView removeFromSuperview];
        self.contentView = nil;
        [self removeFromSuperview];
    }];
    
}

@end
